/*******【注意：本js函数依赖于jquery，在使用时,导入jquery.js库，以及本js （所用样式文件，在本js中自动导入）】***********************************************************/
/**
 * 分页函数：需要开发人员在需要分页的时候调用，调用方式参考示例
 * @param result:结果集对象：该对象包含path(项目根目录)、pageNo、pageSize、totalSize、totalPage、funcname:分页按钮中点击回调函数（）、pageId:分页内容填充的元素id
 * 示例：pagination({         
 * 					"path":"/FK_SECURITYAUDIT",
					"pageNo":currentPageno,
					"pageSize":pageSize,
					"totalSize":itemTotal,
					"funcname":"queryGjfxPageData",
					"pageId":"page_div"
				}).showPagination();
 */
function pagination(param){
	var path=param.path!=undefined?param.path:"";//项目根目录
	//自动导入样式文件,先删后加，防止重复导入
	$("#pagination-css-link").remove();
	$("<link id='pagination-css-link'>").attr({rel:"stylesheet",type:"text/css",href:"/static/plugins/pagination/pagination.css"}).appendTo("head");
	var page=new Object();
	page.currentPage=param.pageNo;//当前页码，默认 1
	page.pageSize=param.pageSize;//页容量，默认 10
	page.totalSize=param.totalSize;//记录总数，默认 0
	page.funcname=param.funcname;//回调函数，
	page.pageId=param.pageId;//分页按钮存放div元素id
	
    /**
     * 显示分页按钮，调用该方法将分页按钮显示在pageId元素区域内
     */
	page.showPagination=function(){
			//参数合法性校验
    		if(this.currentPage==undefined||this.currentPage<0){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（currentPage为空）");
    			return ;
    		}
    		if(this.pageSize==undefined||this.pageSize<0){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（pageSize为空）");
    			return ;
    		}
    		if(this.totalSize==undefined||this.totalSize<0){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（totalSize为空）");
    			return ;
    		}
    		/*if(this.totalPage==undefined||this.totalPage<0){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（totalPage为空）");
    			return ;
    		}*/
    		if(this.funcname==undefined||this.funcname==""){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（funcname为空）");
    			return ;
    		}
    		if(this.pageId==undefined||this.pageId==""){
    			console.log("调用分页组件失败，请检查分页参数的合法性：（pageId为空，或者pageId元素不存在）");
    			return ;
    		}
    		//计算总页数
    		this.totalPage=parseInt((parseInt(this.totalSize)+parseInt(this.pageSize)-1)/parseInt(this.pageSize));
    		
    	 	var pageDiv=$("#"+this.pageId);
    	    pageDiv.empty();
    	    pageDiv.append("<div id='pageNew_div_"+this.funcname+"' class='pageNew_div'></div>");
    	    var p_div=$("#pageNew_div_"+this.funcname+"");
    	    var htm="";
    	    if(this.totalSize>0){
    	    	if(parseInt(this.currentPage)!=1){
    	    		htm+="<input type='button' class='pageNew_l' value=' ' onclick='"+this.funcname+"("+(this.currentPage-1)+","+this.pageSize+")' title='第"+(this.currentPage-1)+"页'/>";
    	    	}else{
    	    		htm+="<input type='button' class='pageNew_l' value=' ' onclick='"+this.funcname+"("+this.currentPage+","+this.pageSize+")'  title='第"+1+"页'/>";
    	    	}
    	    	for(var i=1;i<=parseInt(this.totalPage);i++){
    	    		//当前页
    	    		if(i==parseInt(this.currentPage)){
    	    			htm+="<input  a='1'  type='button' class='pageNew_now' value='"+i+"' onclick='"+this.funcname+"("+i+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    		}
    	    		//当前页的前两页
    	    		if(parseInt(this.currentPage)>i&&i>(parseInt(this.currentPage)-3)&&i!=parseInt(this.currentPage)&&i!=1){
    	    			htm+="<input a='2' type='button' class='pageNew_other' value='"+i+"' onclick='"+this.funcname+"("+i+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    		}
    	    		//当前页的后两页
    	    		if((parseInt(this.currentPage)+3)>i&&i>parseInt(this.currentPage)&&i!=parseInt(this.currentPage)){
    	    			htm+="<input a='3' type='button' class='pageNew_other' value='"+i+"' onclick='"+this.funcname+"("+i+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    		}
    	    		//排头两页
    	    		if(parseInt(this.currentPage)>1&&i==1){
    	    			htm+="<input a='4' type='button' class='pageNew_other' value='"+i+"' onclick='"+this.funcname+"("+i+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    		}
    	    		if(i<=(parseInt(this.currentPage)-3)&&i==(parseInt(this.currentPage)-3)&&i!=1&&i!=parseInt(this.currentPage)){
    	    			htm+="<input a='4' type='button' class='pageNew_other' value='...'  />";
    	    		}
    	    		//排尾两页
    	    		if((parseInt(this.currentPage)+3)<parseInt(this.totalPage)&&i==parseInt(this.totalPage)-2&&i!=parseInt(this.currentPage)){
    	    			htm+="<input a='5' type='button' class='pageNew_other' value='...'/>";
    	    		}
    	    		if((parseInt(this.currentPage)+3)<=parseInt(this.totalPage)&&i<=parseInt(this.totalPage)&&i>(parseInt(this.totalPage)-1)&&i!=parseInt(this.currentPage)){
    	    			htm+="<input a='6' type='button' class='pageNew_other' value='"+i+"' onclick='"+this.funcname+"("+i+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    		}
    	    	}
    	    	if(parseInt(this.currentPage)!=parseInt(this.totalPage)){
    	    		htm+="<input type='button' class='pageNew_r' value=' ' value='"+i+"' onclick='"+this.funcname+"("+(parseInt(this.currentPage)+1)+","+this.pageSize+")'  title='第"+i+"页'/>";
    	    	}else{
    	    		htm+="<input type='button' class='pageNew_r' value=' ' value='"+i+"' onclick='"+this.funcname+"("+this.currentPage+","+this.pageSize+")' title='第"+i+"页'/>";
    	    	}
    	    	htm+="<img style='width:40px;font-weight:normal;border:0px;'/>"+
    	       	"共 "+this.totalSize+" 条 "+this.totalPage+" 页，每页 "+this.pageSize+" 条，"+
    	       	"跳转到：<input type='text' value='' class='pageNew_t' id='pageNew_"+this.funcname+"' onkeyup='checkGoPage("+this.totalPage+",\""+this.funcname+"\")'/> "+
    	       	"<input type='button' value='GO' id='go_"+this.funcname+"' onclick='gotopage("+this.pageSize+","+this.totalSize+","+this.totalPage+",\""+this.funcname+"\")' class='pageNew_go'>";	
    	    }
    	    p_div.append(htm);
    };
    return page;
}

//指定跳转某一页
function gotopage(pageSize,tatalSize,totalPage,funcname){
	var pageNew=$.trim($("#pageNew_"+funcname+"").val());
	var matchNum=new RegExp("^[1-9][0-9]*$");
	if(pageNew!=""&&matchNum.test(pageNew)&&(pageNew>0&&pageNew<=totalPage)){
		$("#hide_currentPage_"+funcname).remove();
		$("#go_"+funcname).after("<input type='hidden' id='hide_currentPage_"+funcname+"' onclick='"+funcname+"("+pageNew+","+pageSize+")'/>");
		$("#hide_currentPage_"+funcname).click();
		$("#go_"+funcname+"").removeAttr("title");
		$("#go_"+funcname+"").removeClass("pageNew_go_wrong").addClass("pageNew_go").val("GO");
	}else{
		$("#go_"+funcname+"").attr("title","页码不正确,请重新填写!");
		$("#go_"+funcname+"").removeClass("pageNew_go").addClass("pageNew_go_wrong").val("×");
	}
};
//输入页面校验
function checkGoPage(totalPage,funcname){
	var pageNew=$.trim($("#pageNew_"+funcname+"").val());
	var matchNum=new RegExp("^[1-9][0-9]*$");
	if(pageNew!=""&&matchNum.test(pageNew)&&(pageNew>0&&pageNew<=totalPage)){
		$("#go_"+funcname+"").removeAttr("title");
		$("#go_"+funcname+"").removeClass("pageNew_go_wrong").addClass("pageNew_go").val("GO");
	}else{
		if(pageNew==""){
			$("#pageNew_"+funcname+"").focus();
			$("#go_"+funcname+"").removeAttr("title");
			$("#go_"+funcname+"").removeClass("pageNew_go_wrong").addClass("pageNew_go").val("GO");
		}else{
			$("#go_"+funcname+"").attr("title","页码不正确,请重新填写!");
			$("#go_"+funcname+"").removeClass("pageNew_go").addClass("pageNew_go_wrong").val("×");
		}
	}
};


